import 'dart:io';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter2/colors.dart';
import 'package:flutter2/ui/splash.dart';
import 'package:flutter2/utils/sp.dart';
import 'package:flutter2/utils/string.dart';
import 'package:flutter2/utils/system_bar.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

final routerObserver = RouteObserver<PageRoute>();

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (Platform.isAndroid) {
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
  }
  await SPUtil.init();
  runApp(const MyApp());
  EasyLoading.instance
    ..loadingStyle = EasyLoadingStyle.custom
    ..boxShadow = [BoxShadow(color: mainText.withAlpha(20))]
    ..indicatorType = EasyLoadingIndicatorType.ring
    ..backgroundColor = Colors.transparent
    ..indicatorColor = btnColor
    ..textColor = mainText
    ..dismissOnTap = false
    ..radius = 10.0;
  SmartDialog.config.toast = SmartConfigToast(
    debounce: true,
    alignment: Alignment.center,
    displayType: SmartToastType.onlyRefresh,
  );
  EasyRefresh.defaultHeaderBuilder = () => ClassicHeader(
        showMessage: false,
        showText: false,
      );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    precacheImage(AssetImage("bg_login_top".toImage()), context);
    return ScreenUtilInit(
      designSize: Size(375, 1000),
      child: GetMaterialApp(
        title: 'Flutter Demo',
        navigatorObservers: [routerObserver, FlutterSmartDialog.observer],
        theme: ThemeData(
          scaffoldBackgroundColor: mainBg,
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.white, primary: btnColor),
          useMaterial3: false,
          appBarTheme: AppBarTheme(
            systemOverlayStyle: systemUiOverlayStyle,
          ),
        ),
        home: SplashPage(),
        builder: FlutterSmartDialog.init(
          builder: (context, child) {
            child = EasyLoading.init()(context, child);
            return child;
          },
        ),
      ),
    );
  }
}
